namespace RuoVea.CodeGen.Web.Controllers;

using Newtonsoft.Json;
using RuoVea.CodeGen.Server.Models;
using RuoVea.CodeGen.Server.Service;

/// <summary>
/// Sys_TableInfo 控制器
/// </summary>
public partial class SysTableInfoController : Controller
{
    /// <summary>
    /// 表信息服务
    /// </summary>
    private readonly SysTableInfoService _sysTableInfoService;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="sysTableInfoService">表信息服务</param>
    public SysTableInfoController(SysTableInfoService sysTableInfoService)
    {
        _sysTableInfoService = sysTableInfoService;
    }

    /// <summary>
    /// 表信息列表页面
    /// </summary>
    /// <returns>视图</returns>
    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <returns>分页结果</returns>
    [HttpPost("tableInfo/pages")]
    public async Task<RestfulResult<PageResult<Sys_TableInfo>>> GetPagesAsync()
    {
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception("未提供上传数据。请在Request.Body中携带上传数据。");
        PageParam? data = JsonConvert.DeserializeObject<PageParam>(dataStr);
        data ??= new PageParam { PageNo = 1, PageSize = 10 };

        var query = await _sysTableInfoService.GetPagesAsync(data);
        return query;
    }

    /// <summary>
    /// 查询所有
    /// </summary>
    /// <returns>表信息列表</returns>
    [HttpGet("tableInfo/list")]
    public async Task<RestfulResult<List<Sys_TableInfo>>> GetlistAsync()
    {
        var restful = await _sysTableInfoService.GetlistAsync();
        return restful;
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data">表信息</param>
    /// <returns>操作结果</returns>
    [UnitOfWork]
    [HttpPost("tableInfo/add")]
    public async Task<RestfulResult> AddAsync([FromBody] Sys_TableInfo data)
    {
        var restful = await _sysTableInfoService.AddAsync(data);
        return restful;
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data">表信息</param>
    /// <returns>操作结果</returns>
    [HttpPost("tableInfo/update")]
    public async Task<RestfulResult> UpdateAsync([FromBody] Sys_TableInfo data)
    {
        var restful = await _sysTableInfoService.UpdateAsync(data);
        return restful;
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data">删除参数</param>
    /// <returns>操作结果</returns>
    [HttpDelete("tableInfo/delete")]
    public async Task<RestfulResult> DeleteAsync([FromBody] dynamic data)
    {
        var restful = await _sysTableInfoService.DeleteAsync(data);
        return restful;
    }
}